﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ClasesLibreria.dao;
using ClasesLibreria.dominio;
using System.Web.Security;

public partial class Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Trace.IsEnabled)
        {
            Trace.Write("Traceeeeeeeeee", "jojojoj");
        }
    }
    protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
    {
        Usuario usuario = DAOUsuarios.getUsuarioByNombre(Login1.UserName, Database.connectionString());
        if (usuario != null && Login1.Password == usuario.Contraseña)
        {
            Session["usuario"] = usuario;
            FormsAuthenticationTicket autTicket = new FormsAuthenticationTicket(1, Login1.UserName.ToString(), DateTime.Now, DateTime.Now.AddMinutes(60), false, usuario.IdRol.Nombre);
            string encrAutTicket = FormsAuthentication.Encrypt(autTicket);
            HttpCookie autCookie = new HttpCookie(".Test", encrAutTicket);
            Response.Cookies.Add(autCookie);
            Response.Redirect(FormsAuthentication.GetRedirectUrl(Login1.UserName.ToString(), false));
        }
        else
        { e.Authenticated = false; }

    }
}